package com.tingyuge.base.thread.wang.chapter9;

import java.util.Arrays;

/**
 * Created by xiaodao
 * date: 2019/11/29
 */
public class ThreadGroupApi {

    public static void main(String[] args) {
        ThreadGroup g1 = new ThreadGroup("g1");
        ThreadGroup g2 = new ThreadGroup(g1,"g2");

        Thread t1 = new Thread(g1,"t1"){
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        System.out.println("--11-");
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        Thread t2 = new Thread(g2,"t1"){
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        System.out.println("--22-");
                        e.printStackTrace();
                        break;
                    }
                }

            }
        };
        t1.start();
        t2.start();

        System.out.println("g1 "+ g1.activeCount());
        System.out.println("g1  "+ g1.activeGroupCount());
        System.out.println("--------------");
        Thread[] ts1 = new Thread[g1.activeCount()];
        System.out.println(ts1.length);
        //copy 线程到数组里.
        g1.enumerate(ts1);
        Arrays.asList(ts1).stream().forEach(System.out::println);
        //打断.
//        g1.interrupt();
        //如果设置成守护的.里面的线程执行完成之后.线程组就会distroy
        g1.setDaemon(true);
    }
}
